package com.zhf.rabbitmq.pubsub;

import com.rabbitmq.client.*;
import com.zhf.rabbitmq.utils.RabbitConstant;
import com.zhf.rabbitmq.utils.RabbitUtil;

import java.io.IOException;

/**
 * @author: 曾鸿发
 * @create: 2022-01-08 22:04
 * @description：
 **/
public class Sina {

    public static void main(String[] args) throws Exception{
        // 获取TCP长连接
        Connection connection = RabbitUtil.getConnection();
        final Channel channel = connection.createChannel();
        // 声明队列信息
        channel.queueDeclare(RabbitConstant.QUEUE_SINA, false, false, false, null);
        // 队列绑定交换机
        // 参数1：队列名， 参数2：交换机名， 参数3：路由key（暂时用不到）
        channel.queueBind(RabbitConstant.QUEUE_SINA, RabbitConstant.EXCHANGE_WEATHER, "");
        channel.basicQos(1);
        channel.basicConsume(RabbitConstant.QUEUE_SINA, false, new DefaultConsumer(channel){

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("新浪天气收到气象信息： " + new String(body));
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        });
    }
}
